<template>
  <div class="listview">
    <scroll
      class="singer-wrapper"
      ref="singerWrapper"
      :probeType="3"
      :data="data"
      :listenScroll="listenScroll"
      @scroll="scroll"
    >
    <!-- 左侧歌手列表 -->
      <ul>
        <li
          v-for="group in data"
          class="singer-group"
          :key="group.title"
          ref="singerGroup"
        >
          <h2 class="title">{{group.title}}</h2>
          <uL>
            <li
              class="singer-item"
              v-for="item in group.items"
              :key="item.id"
              @click="selectItem(item)"
            >
              <img class="avatar" v-lazy="item.avatar">
              <span class="name">{{item.name}}</span>
            </li>
          </uL>
        </li>
      </ul>
        <!-- 右侧字母列表 -->
        <div
          class="list-shortcut"
          @touchstart="onShortcutTouchStart"
          @touchmove.stop.prevent="onShortcutTouchMove"
        >
          <ul>
            <li
              class="item"
              v-for="(item, index) in shortcutList"
              :key="index"
              :data-index="index"
              :class="{'current':currentIndex === index}"
            >
              {{item}}
            </li>
          </ul>
        </div>
        <!-- 固定标题 -->
       <div
        v-show='fixedTitle'
        class="list-fixed"
        ref="fixedTitle"
       >
        <h1 class="fixed-title">{{fixedTitle}}</h1>
       </div>
        <!-- 正在加载 -->
      <div v-show='!data.length' class="loading"><loading /></div>
    </scroll>
  </div>
</template>

<script>
import Scroll from '../../components/scroll/Scroll'
import Loading from '../../components/loading/Loading'
import { getData } from '../../assets/js/dom'

const TITLE_HEIGHT = 30
const ANCHOR_HEIGHT = 18

export default {
  props: {
    data: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      currentIndex: 0,
      scrollY: -1,
      diff: -1
    }
  },
  computed: {
    // 字母列表
    shortcutList () {
      return this.data.map(group => group.title.substr(0, 1))
    },
    // 固定标题
    fixedTitle () {
      // 滚动到最顶端，再弹性下拉时，隐藏
      if (this.scrollY > 0) {
        return ''
      }
      return this.data[this.currentIndex] ? this.data[this.currentIndex].title : ''
    }
  },
  created () {
    this.listHeight = []
    this.touch = {}
    this.listenScroll = true
  },
  methods: {
    onShortcutTouchStart (e) {
      let anchorIndex = getData(e.target, 'index')
      let firstTouch = e.touches[0]
      this.touch.y1 = firstTouch.pageY
      this.touch.anchorIndex = anchorIndex
      this._scrollTo(anchorIndex)
    },
    onShortcutTouchMove (e) {
      let firstTouch = e.touches[0]
      this.touch.y2 = firstTouch.pageY
      let distance = (this.touch.y2 - this.touch.y1) / ANCHOR_HEIGHT | 0
      let anchorIndex = parseInt(this.touch.anchorIndex) + distance
      this._scrollTo(anchorIndex)
    },
    scroll (pos) {
      this.scrollY = pos.y
    },
    selectItem (item) {
      this.$emit('select', item)
    },
    _scrollTo (index) {
      if (!index && index !== 0) {
        return
      }
      if (index < 0) {
        index = 0
      } else if (index > this.listHeight.legth - 2) {
        index = this.listHeight.legth - 2
      }
      this.scrollY = -this.listHeight[index]
      this.$refs.singerWrapper.scrollToElement(this.$refs.singerGroup[index])
    },
    _calculateHeight () {
      this.listHeight = []
      let list = this.$refs.singerGroup
      let height = 0
      this.listHeight.push(height)
      for (let i = 0; i < list.length; i++) {
        let item = list[i]
        height += item.clientHeight
        this.listHeight.push(height)
      }
    }
  },
  watch: {
    data () {
      setTimeout(() => {
        this._calculateHeight()
      }, 20)
    },
    scrollY (newY) {
      let scrollY = Math.abs(Math.round(newY))
      let listHeight = this.listHeight
      for (let i = 0; i < listHeight.length; i++) {
        if (listHeight[i + 1]) {
          if (scrollY >= listHeight[i] && scrollY < listHeight[i + 1]) {
            this.currentIndex = i
            this.diff = listHeight[i + 1] + newY
          }
        }
      }
    },
    diff (newVal) {
      // title重叠的高度
      let fixedTop = (newVal > 0 && newVal < TITLE_HEIGHT) ? newVal - TITLE_HEIGHT : 0
      if (this.fixedTop === fixedTop) {
        return
      }
      this.fixedTop = fixedTop
      // title向上偏移
      this.$refs.fixedTitle.style.transform = `translate3d(0,${fixedTop}px, 0)`
    }
  },
  components: {
    Scroll,
    Loading
  }
}
</script>

<style lang="stylus" scoped>
@import '~@/assets/stylus/variable'
.listview
  position: relative
  width: 100%
  height: 100%
  background: $color-background
  .singer-wrapper
    height: 100%
    overflow: hidden
    .singer-group
      padding-bottom: 30px
      .title
        width: 100%
        box-sizing: border-box
        height: 30px
        line-height: 30px
        padding-left: 20px
        background: $color-highlight-background
        color: $color-text-1
        font-size: 12px
      .singer-item
        display: flex
        align-items: center
        padding: 20px 0 0 30px
        .avatar
          width: 50px
          height: 50px
          border-radius: 50%
        .name
          margin-left: 20px
          color: $color-text-l
          font-size: $font-size-medium
    .list-shortcut
      position: absolute
      z-index: 30
      right: 0
      top: 50%
      transform: translateY(-50%)
      width: 20px
      padding: 20px 0
      border-radius: 10px
      text-align: center
      background: $color-background-d
      font-family: Helvetica
      .item
        padding: 3px
        line-height: 1
        color: $color-text-l
        font-size: $font-size-small
        &.current
          color: $color-theme
    .list-fixed
      position: absolute
      top: 0
      left: 0
      width: 100%
      .fixed-title
        box-sizing: border-box
        height: 30px
        line-height: 30px
        padding-left: 20px
        background: $color-highlight-background
        color: $color-text-1
        font-size: 12px
    .loading
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)
</style>
